package com.it.service.impl;

import com.it.Util.SqlSessionFactoryUtils;
import com.it.mapper.BrandMapper;
import com.it.pojo.Brand;
import com.it.pojo.PageBean;
import com.it.service.BrandService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {

    SqlSessionFactory sqlSessionFactory =  SqlSessionFactoryUtils.getSqlSessionFactory();


    @Override
    public List<Brand> selectAll() {
        //获取sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //执行sql
        List<Brand> brands = mapper.selectAll();
        //释放资源
        sqlSession.close();

        return brands;
    }

    @Override
    public void add(Brand brand) {
        //获取sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //执行sql
        mapper.add(brand);
        //提交表单
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Override
    public void deleteByIds(int[] ids) {
        //获取sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //执行sql
        mapper.deleteByIds(ids);
        //提交表单
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        //获取sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);


        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        //计算查询条目数
        int size = pageSize;

        List<Brand> rows = mapper.selectByPage(begin, size);

        int i = mapper.selectTotalCount();

        PageBean<Brand> brandPageBean = new PageBean<>();
        brandPageBean.setRows(rows);
        brandPageBean.setTotalCount(i);

        return brandPageBean;
    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {

        //获取sqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取mapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);


        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        //计算查询条目数
        int size = pageSize;

        //处理查询条件 模糊表达式
        String brandName = brand.getBrandName();
        if (brandName!=null && brandName.length()>0){
             brand.setBrandName("%"+brandName+"%");
        }

        String companyName = brand.getCompanyName();

        if (companyName != null && companyName.length()>0){
            brand.setCompanyName("%"+companyName+"%");
        }

        List<Brand> rows = mapper.selectByPageAndCondition(begin, size,brand);

        int i = mapper.selectTotalCountByCondition(brand);


        PageBean<Brand> brandPageBean = new PageBean<>();
        brandPageBean.setRows(rows);
        brandPageBean.setTotalCount(i);

        sqlSession.close();

        return brandPageBean;

    }


}
